// ActionScript file
import com.champChooser.constants.CCConstants;
import com.champChooser.constants.ImageConstants;
import com.champChooser.constants.SoundConstants;

import mx.controls.Alert;
import mx.core.Application;
import mx.events.ValidationResultEvent;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.rpc.http.HTTPService;
import mx.utils.StringUtil;

private static const PHP_LOGIN_FORM_PARAM_USERNAME:String="username";
private static const PHP_LOGIN_FORM_PARAM_PASSWORD:String="password";
private static const PHP_LOGIN_FORM_PARAM_FIRSTNAME:String="firstname";
private static const PHP_LOGIN_FORM_PARAM_LASTNAME:String="lastname";
private static const PHP_LOGIN_FORM_PARAM_EMAIL:String="email";
private static const PHP_LOGIN_FORM_PARAM_SEX:String="sex";

private var service:HTTPService ;

public function useRegistrationHttpService(parameters:Object):void {
    service = new HTTPService();
    service.url="http://"+CCConstants.SERVER_IP+"/ChampChooser/core/services/user/registrationService.php";
    service.useProxy = false;
    service.method = "POST";
    
    service.addEventListener("result", httpRegistrationResult);
    service.addEventListener("fault", httpFault);
    service.send(parameters);
}

public function httpRegistrationResult(event:ResultEvent):void {
    var result:Object = event.result;
    if(result.result==0){
    	handleRegisterFailure();
    }else{
    	handleRegisterSuccess(result);
    }
}

private function doRegister():void{
	trace("in register()");
	
	var vResult:ValidationResultEvent;
	
	vResult = valTxtPassword.validate();	
	if (vResult.type==ValidationResultEvent.INVALID) 
    	return;
    vResult = valTxtUsername.validate();	
	if (vResult.type==ValidationResultEvent.INVALID) 
    	return;
    
	vResult = valTxtEmail.validate();	
	if (vResult.type==ValidationResultEvent.INVALID) 
    	return;
    vResult = valTxtFirstname.validate();	
	if (vResult.type==ValidationResultEvent.INVALID) 
    	return;
    vResult = valTxtLastname.validate();	
	if (vResult.type==ValidationResultEvent.INVALID) 
    	return;
    
    vResult = valTxtPasswordReenter.validate();	
	if (vResult.type==ValidationResultEvent.INVALID) 
    	return;
    if(txtPassword.text!=txtPasswordReenter.text){
    	txtPasswordReenter.text="";
    	valTxtPasswordReenter.validate();
    	valTxtPasswordReenter.requiredFieldError = "Passwords entered do not match";
    	return;
    }
	
	var params:Object = {};
	params[PHP_LOGIN_FORM_PARAM_USERNAME] = StringUtil.trim(txtUsername.text);
	params[PHP_LOGIN_FORM_PARAM_PASSWORD] = StringUtil.trim(txtPassword.text);
	params[PHP_LOGIN_FORM_PARAM_FIRSTNAME] = StringUtil.trim(txtFirstname.text);
	params[PHP_LOGIN_FORM_PARAM_LASTNAME] = StringUtil.trim(txtPassword.text);
	params[PHP_LOGIN_FORM_PARAM_EMAIL] = StringUtil.trim(txtPassword.text);
	params[PHP_LOGIN_FORM_PARAM_SEX] = sexType.selectedValue;
	useRegistrationHttpService(params);
}

private function handleRegisterSuccess(result:Object):void{
	this.currentState = "";
	lblLoginFormHeader.text = "Yahoo!! You are registered in now! Login to enter.";
}

public function useLoginHttpService(parameters:Object):void {
    service = new HTTPService();
    service.url="http://"+CCConstants.SERVER_IP+"/ChampChooser/core/services/user/loginService.php";
    service.useProxy = false;
    service.method = "POST";
    
    service.addEventListener("result", httpLoginResult);
    service.addEventListener("fault", httpFault);
    service.send(parameters);
}

public function httpLoginResult(event:ResultEvent):void {
    var result:Object = event.result;
    if(result.records.resultsize==0){
    	handleLoginFailure();
    }else{
    	handleLoginSuccess(result);
    }
}

private function doLogin():void{
	trace("in doLogin()");
	
	var vResult:ValidationResultEvent;
	
	vResult = valTxtPassword.validate();	
	if (vResult.type==ValidationResultEvent.INVALID) 
    	return;
    vResult = valTxtUsername.validate();	
	if (vResult.type==ValidationResultEvent.INVALID) 
    	return;
	
	var params:Object = {};
	params[PHP_LOGIN_FORM_PARAM_USERNAME] = StringUtil.trim(txtUsername.text);
	params[PHP_LOGIN_FORM_PARAM_PASSWORD] = StringUtil.trim(txtPassword.text);
	useLoginHttpService(params);
}

private function handleLoginSuccess(result:Object):void{
	lblLoginFormHeader.text = "Yahoo!! You are logged in now!";
	Application.application.currentState = "loggedInState";
	Application.application.initLoggedInUser(result);
}

private function handleLoginFailure():void{
	lblLoginFormHeader.text = "OOPS... Login Failed!!";
	SoundConstants.GUNSHOT.play()
	Alert.show("","Watch It !!! Login Failed!!",mx.controls.Alert.OK,Application.application.coverFlowScreen,null,ImageConstants.ARE_YOU_DOING_ALL_YOU_CAN);
}

private function handleRegisterFailure():void{
	lblLoginFormHeader.text = "OOPS... Registration Failed!!";
	SoundConstants.GUNSHOT.play()
	Alert.show("","Watch It !!! Registration Failed!!",mx.controls.Alert.OK,Application.application.coverFlowScreen,null,ImageConstants.ARE_YOU_DOING_ALL_YOU_CAN);
}

public function httpFault(event:FaultEvent):void {
    var faultstring:String = event.fault.faultString;
    SoundConstants.GUNSHOT.play()
    Alert.show(faultstring,"Oops.. Some problem occured",mx.controls.Alert.OK,Application.application.coverFlowScreen,null,ImageConstants.ARE_YOU_DOING_ALL_YOU_CAN);
}